package com.tl.libmanager;

import android.content.Context;
import android.util.Log;

/**
 * Created by Administrator on 2017/6/2 0002.
 */

public class PluginManager {
    private static final String TAG = "PluginManager";
    private static PluginManager pluginManager;
    private MainAppInterface mainAppInterface;//appLibrary管理
    private PayEntrance payEntrance;//支付入口

    private PluginManager() {
        mainAppInterface = getImpInstance("com.sr.examination.LibEntrance");
        payEntrance = getImpInstance("com.tl.libpay.LibEntrance");
    }

    public static synchronized PluginManager get() {
        if (pluginManager == null) {
            pluginManager = new PluginManager();
        }
        return pluginManager;
    }

    /**
     * 反射获取类实力
     *
     * @param clazzName
     * @param <T>
     * @return
     */
    private static <T> T getImpInstance(String clazzName) {
        try {
            Class<?> clazz = Class.forName(clazzName);
            if (clazz != null) {
                return (T) clazz.newInstance();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }


    public MainAppInterface getMainAppEntrance() {
        mountable(mainAppInterface);
        return mainAppInterface;
    }

    public PayEntrance getPayEntrance() {
        mountable(payEntrance);
        return payEntrance;
    }

    /**
     * 检测模块是否挂载成功
     *
     * @param mountObject
     * @return
     */
    private boolean mountable(Object mountObject) {
        if (mountObject == null) {
            Log.e(TAG, "Related module mount failure");
            return false;
        }
        return true;
    }

    public boolean checkLogin(Context context) {
        if (context != null) {
            MainAppInterface mainAppInterface = getMainAppEntrance();
            if (mainAppInterface != null) {
                return mainAppInterface.checkLogin(context);
            }
        }
        return false;
    }

    public boolean debuggable() {
        MainAppInterface mainAppInterface = getMainAppEntrance();
        if (mainAppInterface != null) {
            return mainAppInterface.debuggable();
        }
        return false;
    }
}
